#include "ipmsg.h"
#include "server.h"
#include "commonFormats.h"
 int num_of_usr_online ;
 struct HeartBeat heart_beat[200];

int main()
{

    int style;
    int flag;
    int temp;
    char IP[1024] = {0};
    char buffer[1024] = {0};
    char buf[1024] = {0};
    char name[1024] = {0};
    clientRequest client_request;
    pthread_t personal_setting;
    pthread_t Beat;
    struct timeval time;

    temp = 1;

    num_of_usr_online = 0;
    // num_of_usr_online = 0;

  //   Heart();


    printf("%d\n",num_of_usr_online);

    personal_setting = pthread_create(&personal_setting,NULL,(void *)Setting,NULL);
    Beat = pthread_create(&Beat,NULL,(void *)Heart,NULL);

    while(1)
    {
        Recv_Request(buffer,IP,PORT);

   //    printf("%s\n",buffer);

        client_request = Get_client_request(buffer);
        strcpy(client_request.sourceIP,IP);


        style = client_request.requestType;
        //       printf("%d\n",style);

   //     Print(client_request);

        printf("%d\n",style);
        switch(style)
        {
        case 0:                                 //登录
        {
            flag = Is_Login(client_request.senderName,client_request.senderPasswd);             //判断登录是否成功
            if(flag == 0)
            {
                temp = Is_Online(client_request.senderName);                                 //判断该用户是否已经上线
            }
            printf("%d\n",flag);
            printf("%d\n",temp);

            if(temp == 0)
            {
                gettimeofday(&time,NULL);
                strcpy(heart_beat[num_of_usr_online].name,client_request.senderName);
                printf("heart_beat[num_of_usr_online].name:%s\n",heart_beat[num_of_usr_online].name);
                heart_beat[num_of_usr_online].now_time = time.tv_sec;
                heart_beat[num_of_usr_online].last_time = time.tv_sec;

                strcat(buf,"Login_success");
                printf("%s\n",buf);
                Reply_Request(buf, client_request.sourceIP,PORT);

                Change_state(client_request.senderName,'1');
                Write_IP_NAME(client_request);
                Read_message(client_request.senderName,client_request.sourceIP);
                Send_usrinfo(client_request.sourceIP);



                num_of_usr_online++;
            }
            else if(flag == 1)
            {
                strcat(buf,"error");
                printf("%s\n",buf);
                Reply_Request(buf, client_request.sourceIP,PORT);
            }
            else
            {
                strcat(buf,"login");
                printf("%s\n",buf);
                Reply_Request(buf, client_request.sourceIP,PORT);
            }

            break;
        }
        case 1:                                 //注册
        {
            flag = Is_Register(client_request);                                             //判断该用户是否已经被注册
            if(flag == 0)
            {
                strcpy(buf,"Register_success");
                //     Write_IP_NAME(client_request);
                printf("%s\n",client_request.gender);
            }
            else
            {
                //  Reply_Request(buffer, client_request.sourceIP);
                strcpy(buf,"Register_error");
            }
            //   strcpy(client_request.senderIP,"10.1.233.102");
            Reply_Request(buf, client_request.sourceIP,PORT);
            printf("%s\n",buf);
            break;
        }
        case 2:                                 //聊天
        {

            temp = Is_Online(client_request.destName);
            if(temp == 0)
            {
                Save_message(client_request);

                strcpy(name,client_request.senderName);

                bzero(client_request.msg,strlen(client_request.msg));
                strcpy(client_request.msg,"系统提示：该用户不在线，呵呵或者他去洗澡了，呵呵");

                bzero(client_request.senderName,strlen(client_request.senderName));
                strcpy(client_request.senderName,client_request.destName);

                bzero(client_request.destName,strlen(client_request.destName));
                strcpy(client_request.destName,name);


                bzero(client_request.sourceIP,strlen(client_request.sourceIP));
                strcpy(client_request.sourceIP,client_request.destIP);

                bzero(client_request.destIP,strlen(client_request.destIP));
                strcpy(client_request.destIP,IP);

                init_Command( client_request,buffer) ;

                Print(client_request);

                Reply_Request(buffer,client_request.destIP,PORT);

                Print(client_request);

            }
            else
            {
                init_Command( client_request,buffer) ;
                printf("%s\n",buffer);
                Reply_Request(buffer, client_request.destIP,PORT);
            }
            break;
        }
        case 3:                                 //查看留言
        {
            Print(client_request);
            //     Read_message(client_request.sourceIP);
            break;
        }
        case 4:                                 //个人设置
        {

            flag = Personal_setting(client_request);
            //  bzero(buffer,sizeof(buffer));
            if(flag == 0)
            {
                strcat(buf,"success");
            }
            else
            {
                strcat(buf,"error");
            }
            printf("%s\n",buf);
            Reply_Request(buf,client_request.sourceIP,PORT);
            printf("%s\n",client_request.sourceIP);
            printf("%s\n",buf);
            break;
        }
        case 5:                             //退出
        {
            Change_state(client_request.senderName,'0');
            Delete_IP_NAME(client_request);
            //      num_of_usr_online--;
            break;

        }
        case 6:
        {
            temp = Is_Online(client_request.destName);
            if(temp == 0)
            {
                strcpy(buf,"对方不在线");
                Reply_Request(buf, client_request.sourceIP,PORT);
            }
            else
            {
                Reply_Request(buffer, client_request.destIP,PORT);
                printf("buffer:%s\n",buffer);
            }

            break;
        }
        }

        flag = 0;
        temp = 1;
        bzero(buf,strlen(buf));
        bzero(buffer,strlen(buffer));
        bzero(IP,strlen(IP));
    }

    return 0;
}
